//
//  KDetailCollectionCell4.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "KDetailCollectionCell4.h"
#import "FitHeader.h"
#import "FontSizeSingleTon.h"
#import <UIImageView+WebCache.h>
#import "HTTPMethod.h"
#import "changeString.h"
#define FontSize [[FontSizeSingleTon shareFontSize]array]
#import "KVegetableDetailModel.h"
#import "KShiDetailTableCell4.h"
@implementation KDetailCollectionCell4
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        _tableview = [[UITableView alloc]init];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.tableFooterView = [[UIView alloc]init];
        [self creatHeader];
        [self.contentView addSubview:_tableview];
        
        _progressView = [[MBProgressHUD alloc]initWithView:self.contentView];
        _progressView.labelText = @"加载中...";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [self.contentView addSubview:_progressView];
        
    }
    return self;
}
-(void)creatHeader//tablevie头视图的创建
{
    _shicaiImage = [[UIImageView alloc]init];
    _shicaiImage.frame = CGRectMake(35 * wt, 0, (375 - 70) * wt, 170 * ht);
    _tableview.tableHeaderView = _shicaiImage;
    
}
-(void)setID:(NSString *)ID
{
   
    _ID = ID;
    _dataArray = [NSMutableArray array];
    [self getData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    KShiDetailTableCell4 *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[KShiDetailTableCell4 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.lableText = [_dataArray[0] pick];
    cell.model = _dataArray[0];
    return cell;
}
-(void)getData
{
    NSString *postUrlSting = @"http://api.izhangchu.com/";
    NSString *bodyString = [NSString stringWithFormat:@"methodName=MaterialView&material_id=%@&version=4.02",_ID];
    [HTTPMethod getDataByString:postUrlSting BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        KVegetableDetailModel *model = [[KVegetableDetailModel alloc]init];
        [model setValuesForKeysWithDictionary:dataDic];
        [_dataArray addObject:model];
        [_tableview reloadData];
        [_shicaiImage sd_setImageWithURL:[NSURL URLWithString:model.applied_image] placeholderImage:[UIImage imageNamed:@""]];
        [_progressView removeFromSuperview];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11],NSFontAttributeName, nil];
    CGRect rect = [[_dataArray[0] pick] boundingRectWithSize:CGSizeMake(335 * wt, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _tableview.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

@end
